package com.sky.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.IAddressBookService;
import com.sky.service.IOrderDetailService;
import com.sky.service.IOrdersService;
import com.sky.service.IUserService;
import com.sky.utils.CommonUtil;
import com.sky.utils.ThreadLocalUtil;
import com.sky.utils.ValidateCodeUtils;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController("userOrdersController")
@RequestMapping("/user/order")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;
    @Autowired
    private IOrderDetailService iOrderDetailService;
    @PostMapping("/submit")
    public Result submit(@RequestBody Orders order){
        OrderSubmitVO submit = iOrdersService.submit(order);
        //TypeError: Cannot read property 'id' of null
        //正在对一个为null的对象，查看他的id属性
        //发生了报错，所以没有跳转，说明没有传过去数值
        return Result.success(submit);
    }

    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){

//        Integer code = ValidateCodeUtils.generateValidateCode(4);
//        OrderPaymentVO.builder()
//                .paySign("paySign")
//                .packageStr("packageStr")
//                .signType("RSA")
//                .nonceStr("XXXRandom"+code)
//                .timeStamp("timeXXX"+code)
//                .build();

        OrderPaymentVO orderPaymentVO =iOrdersService.payment(ordersPaymentDTO);
    //返回OrderPaymentVO
        String orderNumber = ordersPaymentDTO.getOrderNumber();
        iOrdersService.paySuccess(orderNumber);
        return Result.success(orderPaymentVO);
    }
    @GetMapping("/orderDetail/{orderId}")
    public Result getListByDetail(@PathVariable Long orderId){
        LambdaQueryWrapper<OrderDetail> lqw =new LambdaQueryWrapper<>();
        Orders order = iOrdersService.getById(orderId);
        lqw.eq(OrderDetail::getOrderId,orderId);
        List<OrderDetail> list = iOrderDetailService.list(lqw);
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderDetailList(list);
        BeanUtils.copyProperties(order,orderVO);
        return Result.success(orderVO);
    }

    @GetMapping("/historyOrders")
    public Result page(OrdersPageQueryDTO dto){
        LambdaQueryWrapper<Orders >lqw0 =new LambdaQueryWrapper<>();
        lqw0.eq(Orders::getUserId,ThreadLocalUtil.getValue());
        lqw0.orderByDesc(Orders::getOrderTime);
        IPage<Orders> iPage =new Page<>(dto.getPage(),dto.getPageSize());
        iOrdersService.page(iPage,lqw0);
        List<Orders> records = iPage.getRecords();
        List<OrderVO> voList =new ArrayList<>();
        for (Orders order : records) {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order,orderVO);
            LambdaQueryWrapper<OrderDetail> lqw =new LambdaQueryWrapper<>();
            lqw.eq(OrderDetail::getOrderId,order.getId());
            List<OrderDetail> list = iOrderDetailService.list(lqw);
            List<OrderDetail> orderDetailList =new ArrayList<>();
            for (OrderDetail orderDetail : list) {
                OrderDetail od = new OrderDetail();
                BeanUtils.copyProperties(orderDetail,od);
                orderDetailList.add(od);
            }
            orderVO.setOrderDetailList(orderDetailList);
            voList.add(orderVO);
        }
        //返回一个 iPage<orderVO>
        IPage<OrderVO> voiPage =new Page<>();
        voiPage.setRecords(voList);
        voiPage.setTotal(iPage.getTotal());
        return Result.success(voiPage);
    }

    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        iOrdersService.reminder(id);
        return Result.success();
    }

    @PostMapping("/repetition/{orderId}")
    public Result repetition(@PathVariable Long orderId){
        //再来一单
        //搜索之前的订单和详情订单，并获取，清空id，并添加
        iOrdersService.repetition(orderId);
        return Result.success();
    }
}
